home *** CD-ROM | disk | FTP | other *** search
Text File | 1987-12-31 | 6.2 KB | 218 lines | [TEXT/KAHL] |
- /* mergeIndices XFCN -- 871230-... by ^z ... adapted from earlier
- * non-XFCN mergeIndices work.... call it resource XFCN number 2806 or so.
- *
- * called from HyperCard as: mergeIndices (x0, y0, buffersize)
- *
- * where x0 and y0 are the horizontal and vertical offsets (from the top
- * left screen corner) for the files dialogs, and where the total buffer
- * size is 200kB or so (whatever you have comfortably free when running
- * HyperCard).
- *
- * The function returns nothing if there is an error, but returns "1"
- * on success....
- */
-
-
- /* mergeIndices.1.c -- by Mark^Zimmermann, 19871203-....
- *
- * This little effort lets you merge together two indexed 'brwsr'
- * format databases ... it asks you for two text file names, and
- * then looks for those names with '.k' and '.p' endings, the related
- * 'key' and 'ptr' files containing key words alphabetized and pointers
- * to every occurrence of every word ... if it finds all of them (or
- * if the user gives alternative names for the moved *.k and/or *.p files),
- * it asks for a new name for the merged file, and then proceeds to
- * join the original files together, thus:
- * - the two pairs of index files are merged, with the pointers from
- * the second '.p' file adjusted to allow for the length of the first
- * text file which will be preceding their target words, and with the
- * cumulative count values in the merged '.k' file adjusted to allow
- * for the new words that are in the second text file;
- * - if the merge of key and ptr files is successful, the original
- * '.k' and '.p' files are then deleted;
- * - the second text file is appended to the end of the first (see
- * my old 'append.c' program);
- * - if that merge is successful, the second text file is deleted and
- * the first is renamed to the user's request.
- *
- * This approach still requires a considerable amount of free space
- * on the disk, probably at least equal to the sum of the sizes of the
- * original text files, but at least it more-or-less minimizes the
- * space needed while reducing the risk of leaving the user with
- * indices in a messed-up state if there proves to be insufficient
- * disk space available....
- *
- * The structure of a 'brwsr' index is very simple; see source code
- * of that program for details ... in brief, the text file is plain
- * vanilla text, the key file is an array of 28-letter distinct words
- * in alphabetical order, each one followed by the 4-byte cumulative count
- * of how many words (including that word) have preceded it ... that
- * cumulative count thus gives an offset into the ptr file, which
- * simply consists of 4-byte pointers into the text file for the first
- * character of each word in the index.
- *
- * Written in Lightspeed C ... portions therefore copyright, no doubt....
- * Please send improvements and suggestions to me:
- * science (at) NEMS.ARPA;
- * [75066,2044] on CompuServe;
- * Mark^Zimmermann, 9511 Gwyndale Dr., Silver Spring, MD 20910, USA;
- * telephone 301-565-2166.
- *
- * Thank you! ^z
- */
-
-
- #include "mergeIndices.z.h"
- #include <EventMgr.h>
-
- /* set up global stuff here: first, size of fundamental buffer
- */
-
- long zbufsiz;
-
- /* ...six global buffer pointers to use; need them for merging key and
- * ptr file pairs: four for input, two for output. A single big
- * buffer will serve for merging text files, and it can be defined
- * locally after the key and ptr files are handled, so no need for it
- * here... For each buffer 'buf', have a moving pointer bufp and a
- * counter bufcount. Also define a global variable here to hold the
- * size of the first input text file, so that ptrs can be easily
- * fixed for the second text file's new offset as they are merged in....
- */
- char *buf[3][2], *bufp[3][2];
- long bufcount[3][2], first_text_file_size;
-
- /* ...global file names, vRefs and refNums... numbering scheme is:
- * first subscript is the file name (first in, second in, or merged out)
- * second subscript is the file type (ptr, key, or text)
- * (see header file for #defines to make the subscripts readable)
- */
-
- Str255 fn[3][3];
- int vRef[3][3], refNum[3][3];
-
- /* offsets for the window from the top left screen corner ... 0,0 is
- * fine for the Mac Plus sized screen....
- */
- int x0, y0;
-
- /* ...and a little window to give info to the user...
- */
- WindowRecord w_record;
- WindowPtr info_window;
-
-
- /* main routine to do it all....
- */
-
- pascal void main (paramPtr)
- XCmdBlockPtr paramPtr;
- {
- Handle answer;
- int i,j;
-
- SetUpA4();
- if ( init_everything (paramPtr) &&
- open_all_files () &&
- merge_key_and_ptr_files () &&
- delete_old_key_and_ptr_files () &&
- merge_text_files () &&
- delete_and_rename_text_files () )
- {
- answer = NewHandle (2);
- **answer = '1';
- *(*answer + 1) = '\0';
- paramPtr->returnValue = answer;
- }
- else
- {
- for (i = 0; i < 3; ++i)
- for (j = 0; j < 3; ++j)
- if (refNum[i][j] != 0)
- FSClose (refNum[i][j]);
- for (i = 0; i < 3; ++i)
- for (j = 0; j < 2; ++j)
- if (buf[i][j] != 0)
- DisposPtr (buf[i][j]);
- }
- CloseWindow (info_window);
- RestoreA4();
- return;
- }
-
-
- /* tiny routine to put a message into my message window.... takes in a
- * PASCAL type string and displays it nicely....
- */
-
- void give_msg (msg)
- char *msg;
- {
- Rect b_rect;
-
- if (info_window == 0)
- return;
-
- b_rect.top = b_rect.left = 0;
- b_rect.bottom = b_rect.right = 512;
-
- EraseRect (&b_rect);
- MoveTo (4, 15);
- DrawString (msg);
-
- return;
- }
-
-
- /* function to beep and then wait for a mouse click before returning...
- * delay for 2 ticks to debounce the mouse button a bit .... flush the
- * event queue to keep from having extra clicks sent back to HC when
- * we return....
- */
-
- void beepWait ()
- {
- long tickcount;
-
- SysBeep (10);
- while (! Button())
- ;
- Delay (2L, &tickcount);
- while (Button())
- ;
- Delay (2L, &tickcount);
- FlushEvents (everyEvent, 0);
- return;
- }
-
-
- /* function to convert alphabetic string to a long integer ... from LSC
- * library.... simplified to avoid using isspace() & isdigit() ....
- */
-
- long atol (s)
- register char *s;
- {
- register char signflag = 0;
- register long r = 0;
-
- while ((*s == ' '))
- s++;
-
- if (*s == '-')
- {
- signflag = 1;
- s++;
- }
- else if (*s == '+')
- s++;
-
- while (*s >= '0' && *s <= '9')
- r = r * 10 + (*s++ - '0');
-
- return (signflag ? -r : r);
- }
-
-
-
-